﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DCTAccess.Business;
using DCTLib.Common;
using DCTAccess.Business.Administrator;

public partial class AdminZone_AdminList : System.Web.UI.Page
{
    private string Message
    {
        get { return lblError.Text; }
        set { lblError.Text = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Message = String.Empty;
        if (!IsPostBack)
        {
            try
            {
                BindDataGrid(0);

                if (Session["AdminRegister"] != null)
                {
                    Message = Session["AdminRegister"].ToString();
                }
                Session["AdminRegister"] = null;
            }
            catch
            {
                return;
            }

            this.Form.DefaultButton = this.btnSearch.UniqueID;
            this.Form.DefaultFocus = this.txtFullName.UniqueID;
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        try
        {
            AdministratorFilter filter = GetAdministratorForSearch();
            DataTable dataTable = filter.GetAdministrators();
            if (!ValidationHelper.HasRow(dataTable))
            {
                lblResult.Text = String.Empty;
                DatatableHelper.HasNoRow(dataTable, "FullName", gvSearch);
            }
            else
            {
                lblResult.Text = dataTable.Rows.Count + " result(s) found";
                ControlLoader.BindDatagrid(gvSearch, filter.GetAdministrators(), 0);
            }
        }
        catch
        {
            Message = AppMessages.CANNOT_SEARCH;
        }
    }

    protected void btnReset_Click(object sender, EventArgs e)
    {
        txtFullName.Text = String.Empty;
        txtUsername.Text = String.Empty;
        BindDataGrid(0);
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect(AppUrls.AdminZone.ADMIN_REGISTER);
    }

    protected void ddlRole_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        //get the selected adminID will be deleted
        string[] paramID = Request.Params.GetValues("checkDel");

        if (paramID == null || paramID.Length < 1)
            return;

        //if have any user been deleted
        bool hasDeletedUser = false;

        //the loop
        for (int i = 0; i < paramID.Length; i++)
        {
            bool isDeleted = false;

            Administrator admin = AdministratorController.GetAdministratorByID(int.Parse(paramID[i]));

            if (admin != null && admin.ID != AppSessionInfo.CurrentAdministrator.ID)
                isDeleted = AdministratorController.Delete(admin);

            if (hasDeletedUser || isDeleted)
                hasDeletedUser = true;
        }

        BindDataGrid(0);

        //if have any user was deleted, then give the alert message
        if (hasDeletedUser)
        {
            lblError.Text = AppMessages.DELETED_SUCCESSFULLY;
        }
        else
        {
            lblError.Text = AppMessages.CANNOT_DELETE;
        }
    }

    private void BindDataGrid(int pageIndex)
    {
        AdministratorFilter filter = GetAdministratorForSearch();
        ControlLoader.BindDatagrid(gvSearch, filter.GetAdministrators(), pageIndex);
        DataTable dataTable = filter.GetAdministrators();
        lblResult.Text = dataTable.Rows.Count + " result(s) found";
    }
    private AdministratorFilter GetAdministratorForSearch()
    {
        AdministratorFilter filter = new AdministratorFilter();
        filter.FullName = txtFullName.Text;
        filter.UserName = txtUsername.Text;
        filter.Role = int.Parse(this.ddlRole.SelectedValue);
        return filter;
    }

    protected void gvSearch_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        AdministratorFilter filter = GetAdministratorForSearch();
        ControlLoader.BindDatagrid(gvSearch, filter.GetAdministrators(), e.NewPageIndex);
    }

    public string DisplayRoleName(object data)
    {
        if (data.ToString() == "1")
            return "Quản trị cấp cao";
        return "Quản trị site";
    }
}
